package com.github.shiqiyue.app.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.shiqiyue.app.api.sapi.req.user.UserPageReq;
import com.github.shiqiyue.app.modules.sys.entity.User;
import com.github.shiqiyue.app.modules.sys.mapper.UserMapper;
import com.github.shiqiyue.app.modules.sys.service.IUserService;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wenyao.wu
 * @since 2018-10-11
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {


    /***
     * 管理员的用户名
     */
    public static final String ADMIN_USERNAME = "admin";

    @Override
    public User findByName(String name) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(User.NAME, name);
        return baseMapper.selectOne(queryWrapper);
    }
    @Override
    public Integer countByName(String name) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(User.NAME, name);
        return baseMapper.selectCount(queryWrapper);
    }

    @Override
    public IPage<User> page(UserPageReq query) {
        return baseMapper.userPage(new Page(query.getCurrent(), query.getSize()), query);
    }

    @Override
    public Integer countByName(String name, Long id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(User.NAME, name);
        queryWrapper.ne(User.ID, id);
        return baseMapper.selectCount(queryWrapper);
    }

}
